package com.solution.common.utils.sms;

import com.alibaba.fastjson.JSONObject;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;
/**
 * 阿里云短信发送接口（阿里大于）
 * @author llp
 */
public class AliyunSmsOldUtils {
	public static void main(String[] args) throws ApiException {
		/**
		String url = "http://gw.api.taobao.com/router/rest";
		String appkey = "23455367";
		String secret = "f347002818b77eba03dd19716051cfa0";
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend( "" );
		req.setSmsType( "normal" );
		req.setSmsFreeSignName( "景旭CQB商城" );
		req.setSmsParamString( "{\"code\":\"123456\",\"product\":\"景旭分销在线\"}" );
		req.setRecNum( "15021977226" );
		req.setSmsTemplateCode( "SMS_14202051" );
		AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
		System.out.println(rsp.getBody());
		*/
		
		System.out.println(sendSms("SMS_14202051", "15021977226", "{\"code\":\"123456\",\"product\":\"景旭分销在线（乐平）\"}"));
	}
	
	
	/**
	 * 发送短信
	 * @param templateId 短信模块id(配置在阿里云平台)
	 * @param recNum (接收短信号码)
	 * @param params (短信模板中要替换的参数，使用json格式，注意双引号转义)
	 * @return
	 * @author lilp
	 */
	public static boolean sendSms(String templateId, String recNum, String params){
		String url = "http://gw.api.taobao.com/router/rest";
		String appkey = "23455367";
		String secret = "f347002818b77eba03dd19716051cfa0";
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend( "" );
		req.setSmsType( "normal" );
		req.setSmsFreeSignName( "景旭分销在线" );
		req.setSmsParamString(params);
		req.setRecNum(recNum);
		req.setSmsTemplateCode(templateId);
		AlibabaAliqinFcSmsNumSendResponse rsp = null;
		try {
			rsp = client.execute(req);
		} catch (ApiException e) {
			e.printStackTrace();
			return false;
		}
		JSONObject json = JSONObject.parseObject(rsp.getBody());
		JSONObject resultJson = (JSONObject)json.get("alibaba_aliqin_fc_sms_num_send_response");
		JSONObject result = (JSONObject)resultJson.get("result");
		System.out.println((boolean)result.get("success"));
		return (boolean)result.get("success");
	}
}
